package com.example.aabb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ZdActivity extends AppCompatActivity {

    private Spinner dateSpinner;
    private RecyclerView recyclerView;
    private BillAdapter2 billAdapter;

    private RelativeLayout rlBack;
    private TextView tvBack;
    private TextView tvTitle;
    private TextView tvCj;
    private Button statisticsButton;
    private TextView statisticsText;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zd_activity);
        rlBack = (RelativeLayout) findViewById(R.id.rl_back);
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvCj = (TextView) findViewById(R.id.tv_cj);
        dateSpinner = (Spinner) findViewById(R.id.date_spinner);
        statisticsButton = (Button) findViewById(R.id.statistics_button);
        statisticsText = (TextView) findViewById(R.id.statistics_text);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        billAdapter = new BillAdapter2();
        recyclerView.setAdapter(billAdapter);


        // 模拟账单数据
        List<Bill> billList = new ArrayList<>();
        billList.add(new Bill("零钱提现", "到中国银行(0015)", 260.00, "2024年11月28日 20:55"));
        billList.add(new Bill("二维码收款", "来自*就", 260.00, "2024年11月28日 20:54"));
        billList.add(new Bill("转账", "转给白洋", -100.00, "2024年11月28日 09:52"));
        billList.add(new Bill("零钱提现", "到中国银行(0015)", 100.00, "2024年11月27日 18:35"));
        billList.add(new Bill("转账", "来自白洋", 10.00, "2024年11月27日 18:32"));
        billList.add(new Bill("转账", "来自白洋", 90.00, "2024年11月27日 18:30"));
        billList.add(new Bill("零钱提现", "到中国银行(0015)", 38.50, "2024年11月27日 17:18"));
        billList.add(new Bill("二维码收款", "来自白洋", 17.00, "2024年11月27日 16:46"));

        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        tvCj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZdActivity.this, QuestionActivity.class);
                intent.putExtra("list", (Serializable)billList);
                startActivity(intent);
            }
        });

        // 设置日期选择器
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            Date date = calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
            dateList.add(sdf.format(date));
            calendar.add(Calendar.MONTH, -1);
        }

        ArrayAdapter<String> dateAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dateList);
        dateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dateSpinner.setAdapter(dateAdapter);

        double totalIncome = 0;
        double totalExpense = 0;
        for (Bill bill : billList) {
            if (bill.getAmount() > 0) {
                totalIncome += bill.getAmount();
            } else {
                totalExpense += Math.abs(bill.getAmount());
            }
        }
        statisticsText.setText("支出: $" + String.format(Locale.getDefault(), "%.2f", totalExpense) + "  收入: $" + String.format(Locale.getDefault(), "%.2f", totalIncome));

        // 统计按钮点击事件
        statisticsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ZdActivity.this, TjActivity.class);
                intent.putExtra("list", (Serializable)billList);
                startActivity(intent);

            }
        });

        // 初始加载数据
        billAdapter.setBillList(billList);
    }
}

